
  Rem VBScript To center probe outside diameter Metric Version
  
CurrentFeed = GetOemDRO(818) 'Get the current feedrate to return to later
CurrentAbsInc = GetOemLED(48) 'Get the current G90/G91 state
CurrentGmode = GetOemDRO(819) 'Get the current G0/G1 state
CurrentToolDiameter = GetOemDRO(43) 'gets the current tool diameter

If GetOemLed (825) <> 0 Then 'Check to see if the probe is already grounded or faulty

    Call ProbeGrounded()
    Exit Sub

Else

    XCurrent = GetDro(0)
    YCurrent = GetDro(1)
    OutsideDiameter = GetUserDRO(1152)

    Code "G4 P1" 'Pause 1 second to give time to position probe plate
    While IsMoving ()
    Wend
    Call SetDro (0,0.000)
    While IsMoving ()
    Wend
    Call SetDro (1,0.000)
    Code "F100" 'slow feed rate to 100mm/sec
    Code "G90 G0 X" &XNew + OutsideDiameter * .7
    Code "G91 G0 Z-15"

Rem Probe Left

    XNew = Xcurrent - OutsideDiameter * .7
    Code "G31 X" &XNew
    While IsMoving() 'wait for the move to finish
    Wend
    XPos1 = GetVar(2000) 'get the probe touch location
    Code "G91 G0 Z15"
    Code "G90 G0 X" &XCurrent 'rapid move back to start point
    Code "G91 G0 X" &XCurrent - OutsideDiameter * .7
    Code "G91 G0 Z-15"

Rem Probe Right

    XNew = XCurrent + 75 'probe 75mm to right
    Code "G31 X" &XNew
    While IsMoving()
    Wend
    XPos2 = GetVar(2000)
    Code "G91 G0 Z15"
    XCenter = (XPos1 + XPos2) / 2 'center is midway between XPos1 and XPos2
    Code "G90 G0 X" &XCenter 'rapid move to the x center location
    While IsMoving ()
    Wend
    Call SetDro (0,0.000)
    Code "G4 P0.25" 
    Code "G90 G0 Y" &YNew - OutsideDiameter * .7
    Code "G91 G0 Z-15"

Rem Probe up

    YNew = YCurrent + 75
    Code "G31 Y" &YNew
    While IsMoving()
    Wend
    YPos1 = GetVar(2001)
    Code "G91 G0 Z15"
    Code "G90 G0 Y" &YCurrent
    Code "G91 G0 Y" &YCurrent + OutsideDiameter * .7
    Code "G91 G0 Z-15"

Rem Probe down

    YNew = YCurrent - 75
    Code "G31 Y" &YNew
    While IsMoving()
    Wend
    YPos2 = GetVar(2001)
    Code "G91 G0 Z15"
    YCenter = (YPos1 + YPos2) / 2

Rem move To the center

    Code "G90 G0 Y" &YCenter
    While IsMoving ()
    Wend
    Call SetDro (1,0.000)
    While IsMoving ()
    Wend
    Call SetUserDro (1152,YPos2 - YPos1 - CurrentToolDiameter)

    Code "G4 P0.25" 

    Code "F" &CurrentFeed 'restore starting feed rate
	Call ReturnG90G91State()
	Exit Sub
End If
 
Sub ProbeGrounded()
	Code "(Probe plate is grounded, check connection and try again)"
	Call ReturnG90G91State()
End Sub

Sub ReturnG90G91State()
	If CurrentAbsInc = 0 Then 'if G91 was in effect before then return to it
		Code "G91"
	End If
	If CurrentGMode = 0 Then 'if G0 was in effect before then return to it
		Code "G0"
	End If
End Sub    


